package com.nobody.facepk.service;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nobody.facepk.dao.AccountDao;
import com.nobody.facepk.dao.FriendsDao;
import com.nobody.facepk.dao.UserDao;
import com.nobody.facepk.model.Account;
import com.nobody.facepk.model.Friends;
import com.nobody.facepk.model.User;
import com.nobody.facepk.model.UserData;
import com.nobody.facepk.model.UserDataType;
import com.trg.dao.hibernate.GeneralDAO;

@Service
public class UserService {
	private GeneralDAO generalDao;
	private AccountDao accountDao;
	private UserDao userDao;
	private FriendsDao friendsDao;

	@Transactional(readOnly = false)
	public User createUser(String mobile, String name, String email,Account accont) {
		User user = this.generalDao.find(User.class, accont.getUserId());
		user.setName(name);
		user.setMobile(mobile);
		user.setEmail(email);
		this.generalDao.save(user);
		return user;
	}
	
	@Transactional(readOnly = false)
	public Account register(String name, String passwd) {
		Account acc = this.accountDao.findAccount(name);
		Account newAcc;
		if (acc == null) {
			//登陆信息
			newAcc = new Account();
			newAcc.setAccount(name);
			newAcc.setPasswd(passwd);
			//创建我的信息
			User user = new User();
			user.setName("");
			user.setMobile("");
			user.setEmail("");
			this.generalDao.save(user);
			newAcc.setUserId(user.getId());
			this.generalDao.save(newAcc);
			//创建我的好友信息
			Friends friends = new Friends();
			friends.setUserId(user.getId());
			this.generalDao.save(friends);
			return newAcc;
		} else {
			return null;
		}
	}
	
	@Transactional(readOnly = false)
	public boolean addFriend(String mobile, long userid) {
		User targetUser = this.userDao.getUserByMobile(mobile);
		if (targetUser != null) {
			long targetUserId = targetUser.getId();
			Friends friends = this.friendsDao.getFriendsByUserid(userid);
			Set<Long> friendIds = friends.getFriendIds();
			if (!friendIds.contains(targetUserId)) {
				friendIds.add(targetUserId);
				this.generalDao.save(friends);
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	@Transactional(readOnly = false)
	public int checkUserByName(String userName){
		Account account = this.accountDao.findAccount(userName);
		if(account !=null){
			return 1;
		}else{
			return 0;
		}
	}
	@Transactional(readOnly = false)
	public Friends getMyFriends(long userId) {
		Friends friends = this.friendsDao.getFriendsByUserid(userId);
		return friends;
	}
	
	@Transactional(readOnly = false)
	public User getUserById(long userid) {
		User user = this.generalDao.find(User.class, userid);
		return user;
	}
	
	@Transactional(readOnly = false)
	public User getUserByMobile(String mobile) {
		User user = this.userDao.getUserByMobile(mobile);
		return user;
	}

	@Transactional(readOnly = false)
	public Account login(String name) {
		Account account = this.accountDao.findAccount(name);
		return account;
	}
	
	@Transactional(readOnly=true)
	public UserData getUserData(Long userId, UserDataType type) {
		return this.userDao.getUserData(userId, type);
	}

	@Autowired
	public void setGeneralDao(GeneralDAO generalDao) {
		this.generalDao = generalDao;
	}

	@Autowired
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Autowired
	public void setFriendsDao(FriendsDao friendsDao) {
		this.friendsDao = friendsDao;
	}

}
